<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>聊天输入框</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f5f6fa;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .chat-container {
            width: 100%;
            max-width: 500px;
            background-color: #ffffff;
            border-radius: 12px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            display: flex;
            flex-direction: column;
        }

        .chat-header {
            background-color: #007bff;
            color: white;
            padding: 15px;
            text-align: center;
            font-size: 18px;
            font-weight: bold;
        }

        .chat-messages {
            flex: 1;
            height: 300px;
            padding: 15px;
            overflow-y: auto;
            background-color: #f9f9fb;
            border-bottom: 1px solid #e0e0e0;
        }

        .message {
            margin-bottom: 10px;
            padding: 8px 12px;
            background-color: #e9ecef;
            border-radius: 18px;
            display: inline-block;
            max-width: 80%;
            word-wrap: break-word;
        }

        /* 工具栏与输入框融合 */
        .input-toolbar {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 12px 15px;
            border-top: 1px solid #e0e0e0;
            background-color: #ffffff;
        }

        .tool-btn {
            background: none;
            border: none;
            font-size: 20px;
            cursor: pointer;
            color: #6c757d;
            transition: all 0.2s ease;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .tool-btn:hover {
            background-color: #f0f0f0;
            transform: scale(1.1);
            color: #007bff;
        }

        /* 融合式输入区域 */
        .input-composite {
            flex: 1;
            display: flex;
            align-items: center;
            background-color: #f1f1f4;
            border-radius: 24px;
            padding: 8px 12px;
            font-size: 16px;
            min-height: 36px;
            max-height: 100px;
            overflow-y: auto;
            outline: none;
            cursor: text;
            color: #333;
            transition: background-color 0.2s;
        }

        .input-placeholder {
            color: #aaa;
            pointer-events: none;
            user-select: none;
        }

        .composite-text {
            flex: 1;
            white-space: pre-wrap;
            word-wrap: break-word;
            outline: none;
            min-height: 1em;
        }

        .send-btn {
            background-color: #007bff;
            color: white;
            border: none;
            width: 80px;
            height: 40px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
            margin-left: auto;
            margin-right: 15px;
        }

        .send-btn:hover {
            background-color: #0056b3;
            transform: scale(1.05);
        }

        .hidden {
            display: none;
        }

        .emoji-picker {
            position: absolute;
            bottom: 100%;
            left: 20px;
            background-color: white;
            border-radius: 12px;
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
            padding: 10px;
            display: grid;
            grid-template-columns: repeat(7, 1fr);
            gap: 8px;
            z-index: 10;
            animation: fadeIn 0.2s ease-out;
        }

        .emoji-picker button {
            font-size: 18px;
            border: none;
            background: none;
            cursor: pointer;
            padding: 5px;
            border-radius: 8px;
            transition: background-color 0.2s;
        }

        .emoji-picker button:hover {
            background-color: #f0f0f0;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .file-preview {
            display: flex;
            gap: 10px;
            padding: 0 15px 10px;
            flex-wrap: wrap;
        }

        .preview-item {
            position: relative;
            width: 80px;
            height: 80px;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
        }

        .preview-item img, .preview-item .file-icon {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .preview-item .file-icon {
            background-color: #007bff;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
        }

        .remove-preview {
            position: absolute;
            top: -8px;
            right: -8px;
            width: 20px;
            height: 20px;
            background-color: #ff4757;
            color: white;
            border: none;
            border-radius: 50%;
            font-size: 12px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    </style>
</head>
<body>
<div class="chat-container">
    <div class="chat-header">聊天应用</div>
    <div class="chat-messages" id="chat-messages">
        <div class="message">你好！这是一个支持表情、图片和文件的融合式聊天输入框。</div>
    </div>

    <!-- 文件预览区域 -->
    <div class="file-preview hidden" id="file-preview"></div>

    <!-- 融合式工具栏与输入框 -->
    <div class="input-toolbar">
        <button class="tool-btn" id="emoji-btn">😊</button>
        <button class="tool-btn" id="image-btn">🖼️</button>
        <button class="tool-btn" id="file-btn">📎</button>
        <button class="tool-btn" id="ai-btn">🤖</button>
        <button class="tool-btn" id="time-btn">⏰</button>
    </div>

    <!-- 融合输入区 -->
    <div class="input-composite" id="input-composite" contenteditable="true"
         placeholder="输入消息..." spellcheck="false"></div>

    <!-- 发送按钮 -->
    <button class="send-btn" id="send-btn">发送</button>
</div>

<script>
    // 获取DOM元素
    const inputComposite = document.getElementById('input-composite');
    const chatMessages = document.getElementById('chat-messages');
    const sendButton = document.getElementById('send-btn');
    const emojiButton = document.getElementById('emoji-btn');
    const imageButton = document.getElementById('image-btn');
    const fileButton = document.getElementById('file-btn');
    const aiButton = document.getElementById('ai-btn');
    const timeButton = document.getElementById('time-btn');
    const filePreview = document.getElementById('file-preview');

    // 表情包列表
    const emojis = ['😀', '😁', '😂', '🤣', '😊', '😍', '🤔', '😎', '😭', '😱', '👍', '👏', '🎉', '❤️', '🙏'];

    // 用于存储临时选择的文件
    let selectedFiles = [];

    // 动态调整输入区域高度
    function adjustInputHeight() {
        inputComposite.style.height = 'auto';
        const height = Math.min(inputComposite.scrollHeight, 100);
        inputComposite.style.height = height + 'px';
    }

    // 检查是否为空并显示占位符
    function updatePlaceholder() {
        if (inputComposite.textContent.trim().length === 0 && selectedFiles.length === 0) {
            inputComposite.setAttribute('data-placeholder', '输入消息...');
        } else {
            inputComposite.removeAttribute('data-placeholder');
        }
    }

    // 发送消息函数
    function sendMessage() {
        const message = inputComposite.textContent.trim();

        if (message || selectedFiles.length > 0) {
            const messageDiv = document.createElement('div');
            messageDiv.classList.add('message');

            // 添加文本消息
            if (message) {
                messageDiv.textContent = message;
            }

            // 添加文件预览到消息
            selectedFiles.forEach(file => {
                const filePreviewInMsg = document.createElement('div');
                filePreviewInMsg.style.marginTop = '5px';
                filePreviewInMsg.style.fontSize = '12px';
                filePreviewInMsg.style.color = '#007bff';
                filePreviewInMsg.textContent = `📎 ${file.name}`;
                messageDiv.appendChild(filePreviewInMsg);
            });

            chatMessages.appendChild(messageDiv);
            chatMessages.scrollTop = chatMessages.scrollHeight;

            // 清空输入和文件
            inputComposite.textContent = '';
            updatePlaceholder();
            clearFilePreview();
        }
    }

    // 点击发送按钮
    sendButton.addEventListener('click', sendMessage);

    // 回车发送，Shift+回车换行
    inputComposite.addEventListener('keydown', function(e) {
        if (e.key === 'Enter') {
            if (!e.shiftKey) {
                e.preventDefault();
                sendMessage();
            }
            // shift+enter自动换行，contenteditable默认支持
        }
    });

    inputComposite.addEventListener('input', function() {
        updatePlaceholder();
    });

    // 创建表情选择器
    function createEmojiPicker() {
        const picker = document.createElement('div');
        picker.classList.add('emoji-picker');

        emojis.forEach(emoji => {
            const btn = document.createElement('button');
            btn.textContent = emoji;
            btn.addEventListener('click', () => {
                // 插入表情到光标位置
                document.execCommand('insertText', false, emoji);
                inputComposite.focus();
                picker.remove();
            });
            picker.appendChild(btn);
        });

        return picker;
    }

    // 显示/隐藏表情选择器
    let emojiPicker = null;
    emojiButton.addEventListener('click', () => {
        if (emojiPicker && document.body.contains(emojiPicker)) {
            emojiPicker.remove();
            emojiPicker = null;
            return;
        }

        // 创建并显示表情选择器
        emojiPicker = createEmojiPicker();
        const toolbar = document.querySelector('.input-toolbar');
        toolbar.appendChild(emojiPicker);
        inputComposite.focus();
    });

    // 图片上传处理
    imageButton.addEventListener('click', () => {
        const input = document.createElement('input');
        input.type = 'file';
        input.accept = 'image/*';
        input.multiple = true;
        input.style.display = 'none';
        input.onchange = (e) => {
            const files = Array.from(e.target.files);
            handleFiles(files);
        };
        document.body.appendChild(input);
        input.click();
        document.body.removeChild(input);
    });

    // 文件上传处理
    fileButton.addEventListener('click', () => {
        const input = document.createElement('input');
        input.type = 'file';
        input.multiple = true;
        input.style.display = 'none';
        input.onchange = (e) => {
            const files = Array.from(e.target.files).filter(f => !f.type.startsWith('image/'));
            handleFiles(files);
        };
        document.body.appendChild(input);
        input.click();
        document.body.removeChild(input);
    });

    // 处理文件选择
    function handleFiles(files) {
        if (files.length === 0) return;

        selectedFiles = selectedFiles.concat(files);
        updateFilePreview();
    }

    // 更新文件预览
    function updateFilePreview() {
        filePreview.classList.remove('hidden');
        filePreview.innerHTML = '';

        selectedFiles.forEach((file, index) => {
            const item = document.createElement('div');
            item.classList.add('preview-item');

            if (file.type.startsWith('image/')) {
                const img = document.createElement('img');
                img.src = URL.createObjectURL(file);
                item.appendChild(img);
            } else {
                const icon = document.createElement('div');
                icon.classList.add('file-icon');
                icon.textContent = '📄';
                item.appendChild(icon);
            }

            const removeBtn = document.createElement('button');
            removeBtn.classList.add('remove-preview');
            removeBtn.textContent = '×';
            removeBtn.onclick = () => {
                selectedFiles.splice(index, 1);
                updateFilePreview();
                if (selectedFiles.length === 0) {
                    filePreview.classList.add('hidden');
                }
            };
            item.appendChild(removeBtn);

            filePreview.appendChild(item);
        });
    }

    // 清空预览
    function clearFilePreview() {
        selectedFiles.forEach(file => {
            if (typeof file.previewUrl !== 'undefined') {
                URL.revokeObjectURL(file.previewUrl);
            }
        });
        selectedFiles = [];
        filePreview.classList.add('hidden');
        filePreview.innerHTML = '';
    }

    // 点击外部关闭表情选择器
    document.addEventListener('click', (e) => {
        if (emojiPicker && !emojiButton.contains(e.target) && !emojiPicker.contains(e.target)) {
            emojiPicker.remove();
            emojiPicker = null;
        }
    });

    // 初始化
    updatePlaceholder();
    inputComposite.focus();
</script>
</body>
</html>